package cc.bluemc.modules.weixin.service;


import cc.bluemc.modules.weixin.entity.WxPublicAccountEntity;
import cc.bluemc.platform.comm.DataTablesPagination;
import cc.bluemc.platform.jdbctemplatetool.JdbcTemplateTool;
import cc.bluemc.platform.jdbctemplatetool.exception.NoColumnAnnotationFoundException;
import cc.bluemc.platform.jdbctemplatetool.exception.NoIdAnnotationFoundException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
*WxPublicAccount service
*/
@Transactional(readOnly=true)
@Service("wxPublicAccountService")
public class WxPublicAccountService {
   	private Log log = LogFactory.getLog(this.getClass());
	@Autowired
	private JdbcTemplateTool jdbcTemplateTool;

  
  	@Transactional(readOnly=false)
    public void save(WxPublicAccountEntity wxPublicAccount) throws Exception{
    	jdbcTemplateTool.save(wxPublicAccount);
    }
	@Transactional(readOnly=false)
    public int saveAndGetId(WxPublicAccountEntity wxPublicAccount) throws Exception{
    	return jdbcTemplateTool.save(wxPublicAccount,"appid");
    }
	@Transactional(readOnly=false)
    public void update(WxPublicAccountEntity wxPublicAccount) throws Exception{
    	jdbcTemplateTool.update(wxPublicAccount);
    }
	@Transactional(readOnly=false)
    public void saveOrUpdate(WxPublicAccountEntity wxPublicAccount) throws Exception {
        if (StringUtils.isBlank(wxPublicAccount.getAppid())) {
            log.error("更新wxPublicAccount，无appId");
        } else {
			if(null==get(wxPublicAccount.getAppid())){
				save(wxPublicAccount);
			}else{
				update(wxPublicAccount);
			}

        }
    }
	public WxPublicAccountEntity get(int userid,String appid) throws NoColumnAnnotationFoundException, NoIdAnnotationFoundException {
		return jdbcTemplateTool.get(" appid=?",new Object[]{appid},WxPublicAccountEntity.class);
	}
    public WxPublicAccountEntity get(String appid) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
    	return jdbcTemplateTool.get(WxPublicAccountEntity.class, appid);
    }
 	/**
     * 所有列表
     */
    public List<WxPublicAccountEntity> getAll() {
    	String sql="SELECT * FROM t_wx_public_account ";
    	return jdbcTemplateTool.list(sql, null, WxPublicAccountEntity.class);
    }
    /**
     * 分页列表
     * @param page
     * @return
     */
    public DataTablesPagination getListOfPage(DataTablesPagination page,int userId){
    	StringBuffer sql = new StringBuffer(" FROM t_wx_public_account WHERE admin_user_id=?");
    	List<Object> params = new ArrayList<Object>();
		params.add(userId);

    	int count =jdbcTemplateTool.count("SELECT count(0)"+sql, params.toArray());
    	page.setiTotalRecords(count);
    	
    	sql.append(" LIMIT ?,?");
    	params.add(page.getiDisplayStart());
    	params.add(page.getiDisplayLength());
    	List<WxPublicAccountEntity> list = jdbcTemplateTool.list("SELECT * "+sql, params.toArray(), WxPublicAccountEntity.class);
    	page.setData(list);
    	return page;
    }
     /**
     * 删除
     * @throws Exception
     */
	@Transactional(readOnly=false)
    public void delete(WxPublicAccountEntity wxPublicAccount) throws Exception {
   	 	jdbcTemplateTool.delete( wxPublicAccount);
    }
}
